package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.Member;
import com.example.poverty.service.MemberService;
import com.example.poverty.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 前端  成员管理  控制类
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 添加成员信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addMember(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String organizationId = request.getParameter("organizationId").trim();  //成员id
        String taskForce = request.getParameter("taskForce").trim();  //工作队
        String name = request.getParameter("name").trim();  //姓名
        String nationality = request.getParameter("nationality").trim();  //民族
        String sex = request.getParameter("sex").trim();  //性别
        String phone = request.getParameter("phone").trim();  //电话
        String politicalStatus = request.getParameter("politicalStatus").trim();  //政治面貌
        String education = request.getParameter("education").trim();  //学历
        String jobTitle = request.getParameter("jobTitle").trim();  //职务

        Member member = new Member();
        member.setOrganizationId(Integer.parseInt(organizationId));
        member.setTaskForce(taskForce);
        member.setName(name);
        member.setNationality(nationality);
        member.setSex(new Byte(sex));
        member.setPhone(phone);
        member.setPoliticalStatus(new Byte(politicalStatus));
        member.setEducation(new Byte(education));
        member.setJobTitle(jobTitle);
        boolean flag = memberService.insert(member);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 修改成员信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateMember(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  //主键
        String taskForce = request.getParameter("taskForce").trim();  //工作队
        String name = request.getParameter("name").trim();  //姓名
        String nationality = request.getParameter("nationality").trim();  //民族
        String sex = request.getParameter("sex").trim();  //性别
        String phone = request.getParameter("phone").trim();  //电话
        String politicalStatus = request.getParameter("politicalStatus").trim();  //政治面貌
        String education = request.getParameter("education").trim();  //学历
        String jobTitle = request.getParameter("jobTitle").trim();  //职务

        Member member = new Member();
        member.setId(Integer.parseInt(id));
        member.setTaskForce(taskForce);
        member.setName(name);
        member.setNationality(nationality);
        member.setSex(new Byte(sex));
        member.setPhone(phone);
        member.setPoliticalStatus(new Byte(politicalStatus));
        member.setEducation(new Byte(education));
        member.setJobTitle(jobTitle);
        boolean flag = memberService.update(member);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "修改失败");
        return jsonObject;
    }

    /**
     * 删除成员信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Object deleteMember(HttpServletRequest request){
        String id = request.getParameter("id").trim();  //主键
        boolean flag = memberService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey",method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request){
        String id = request.getParameter("id").trim();  //主键
        return memberService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询所有成员信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/allMember",method = RequestMethod.GET)
    public Object allMember(HttpServletRequest request){
        return memberService.allMember();
    }

    /**
     * 根据姓名查找成员信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByName",method = RequestMethod.GET)
    public Object getByName(HttpServletRequest request){
        String name = request.getParameter("name").trim();  //姓名
        return memberService.getByName(name);
    }

    /**
     * 根据成员id查询成员信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/memberOfOrganizationId",method = RequestMethod.GET)
    public Object memberOfOrganizationId(HttpServletRequest request){
        String organizationId = request.getParameter("organizationId").trim();  //成员id
        return memberService.memberOfOrganizationId(Integer.parseInt(organizationId));
    }


}
